﻿
// TCPClientDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "TCPClient.h"
#include "TCPClientDlg.h"
#include "afxdialogex.h"
#include <Afxsock.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CTCPClientDlg 对话框



CTCPClientDlg::CTCPClientDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_TCPCLIENT_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTCPClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CTCPClientDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON2, &CTCPClientDlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON1, &CTCPClientDlg::OnBnClickedButton1)
	ON_MESSAGE(WM_SOCKET, &CTCPClientDlg::OnSocket)
END_MESSAGE_MAP()


// CTCPClientDlg 消息处理程序

BOOL CTCPClientDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	//GetDlgItem(IDC_EDIT3)->EnableWindow(false);
	GetDlgItem(IDC_BUTTON1)->EnableWindow(false);

	//初始化套结字动态库 
	WSADATA         wsd;            //WSADATA变量  
	if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
	{
		CString str;
		GetDlgItem(IDC_EDIT3)->GetWindowTextA(str);
		str += "初始化库失败！！！\r\n";
		GetDlgItem(IDC_EDIT3)->SetWindowTextA(str);
	}

	//设置异步通信
	ClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

	if (INVALID_SOCKET == ClientSocket)
	{
		CString str;
		GetDlgItem(IDC_EDIT3)->GetWindowTextA(str);
		str += "初始化失败！！！\r\n";
		GetDlgItem(IDC_EDIT3)->SetWindowTextA(str);
	}

	WSAAsyncSelect(ClientSocket, this->m_hWnd, WM_SOCKET, FD_READ);


	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CTCPClientDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CTCPClientDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CTCPClientDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



afx_msg LRESULT CTCPClientDlg::OnSocket(WPARAM wParam, LPARAM lParam)
{
	//异步套接字处理
	CString str;
	char cs[100] = "";
	int length;

	if (lParam == FD_READ)
	{
		recv(ClientSocket, cs, 100, 0);

		GetDlgItem(IDC_EDIT3)->GetWindowTextA(str);
		CString str1 = "服务器: ";
		str1 += (LPTSTR)cs;
		str1 += "\r\n";
		GetDlgItem(IDC_EDIT3)->SetWindowTextA(str1 + str);
	}
	
	return 0;
}



void CTCPClientDlg::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码
	CString str, str1, str2;
	int port;
	GetDlgItem(IDC_EDIT1)->GetWindowTextA(str);
	GetDlgItem(IDC_EDIT2)->GetWindowTextA(str1);

	if (str == "" || str1 == "")
	{
		GetDlgItem(IDC_EDIT3)->GetWindowTextA(str);
		str = "服务器输入为空！！！\r\n" + str;
		GetDlgItem(IDC_EDIT3)->SetWindowTextA(str);
	}
	else
	{
		port = atoi(str1.GetBuffer(1));
		ServerAddress.sin_family = AF_INET;
		//如果编译通不过 属性 c++ 常规  sdl 改成否
		ServerAddress.sin_addr.s_addr = inet_addr(str);//设置服务端地址  这里表示本机
		ServerAddress.sin_port = htons((short)port);
		int nServAddlen = sizeof(ServerAddress);

		//连接服务器  
		if (connect(ClientSocket, (sockaddr*)&ServerAddress, sizeof(sockaddr_in)))
		{
			GetDlgItem(IDC_EDIT3)->GetWindowTextA(str2);
			str2 = "连接服务器成功！！！\r\n" + str;
			GetDlgItem(IDC_EDIT3)->SetWindowTextA(str2);
			GetDlgItem(IDC_BUTTON1)->EnableWindow(true);
		}
		else
		{
			GetDlgItem(IDC_EDIT3)->GetWindowTextA(str);
			str = "连接服务器失败！！！\r\n" + str;
			GetDlgItem(IDC_EDIT3)->SetWindowTextA(str);
		}
	}
}


void CTCPClientDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	CString str, str1;

	GetDlgItem(IDC_EDIT4)->GetWindowTextA(str);
	if (str == "")
	{
		GetDlgItem(IDC_EDIT3)->GetWindowTextA(str1);
		str1 = "发送消息不能为空！！！\r\n" + str;
		GetDlgItem(IDC_EDIT3)->SetWindowTextA(str1);
	}
	else
	{
		send(ClientSocket, str.GetBuffer(1), str.GetLength(), 0);

		GetDlgItem(IDC_EDIT3)->GetWindowTextA(str1);
		str1 = "\r\n客户端发送:" + str + "\r\n" + str1;
		GetDlgItem(IDC_EDIT3)->SetWindowTextA(str1);
	}
}
